/*!
 * XTable v0.0.1
 * Copyright 2018-06-19 XTable
 * 作者: 向涛
 * XTable  一个表格显示框架
 */
var XTable = (function(){
	XTable.NORMAL = 0; //普通
	XTable.LEFT_CASCADE = 1; //级联
	//构造方法
	function XTable(){

	}

	var _prote = XTable.prototype;
	
	/**
	 * 设置参数 顺便计算每个元素的子元素个数，生成Html时rowspan要用到
	 * @method setOption
	 * @param {option} 参数.
	 * @return
	 **/
	_prote.setOption = function(option){
		switch(option.type){
			case XTable.NORMAL:
				this.option = option;
				break;
			case XTable.LEFT_CASCADE:
				this.option = this.setLeftCascadeSize(option);
				break;
		}
	}
	/**
	 * 获取参数
	 * @method getOption
	 * @return {option}
	 **/
	_prote.getOption = function(){
		return this.option;
	}
	/**
	 * 将其生成的Html在table中显示
	 * @method show
	 * @param {id} table的id.
	 * @return
	 **/
	_prote.show = function(id){
		switch(this.option.type){
			case XTable.NORMAL:
				this.renderThead(id);
				this.renderTbodyNormal(id);
				break;
			case XTable.LEFT_CASCADE:
				this.renderThead(id);
				this.renderTbodyLeftCascade(id);
				break;
		}
	}
	_prote.renderThead = function(id){
		if(this.option.head){
			var thead = document.createElement("thead");
			var tr = "<tr>"
			for(var i = 0; i < this.option.head.length; i ++){
				head = this.option.head[i];
				var style = "";
				var stclass = "";
				if(head.style){
					style = head.style;
				}
				if(head.class){
					stclass = head.class;
				}
				tr += "<th class='"+stclass+"' style='"+style+"'>"+head.name+"</th>";
				
			}
			tr += "</tr>";
			$(thead).append(tr);
			$("#" + id + " thead").html("");
			$("#" + id).append(thead);
		}
	}
	_prote.renderTbodyNormal = function(id){
		var tbody = document.createElement("tbody");
		var tbodyId = "xttbody" + Math.floor(Math.random() * 1000);
		$(tbody).attr("id", tbodyId);
		$("#" + id + " tbody").html("");
		$("#" + id).append(tbody);
		this.renderNormal(tbodyId);
	}
	_prote.renderTbodyLeftCascade = function(id){
		var tbody = document.createElement("tbody");
		var tbodyId = "xtbody"+ Math.floor(Math.random() * 1000);
		$(tbody).attr("id", tbodyId)
		$("#" + id).append(tbody);
		this.renderLeftCascade(tbodyId);
	}
	/**
	 * 为ID元素添加数据项
	 * @method renderNormal
	 * @param {option} 参数.
	 * @return
	 **/
	_prote.renderNormal = function(id){
		var data = this.option.data;
		$("#" + id).html("");
		for(var i = 0; i < data.length; i ++){
			var trHtmlElement = document.createElement("tr");
			if(this.option.showKey){
				for(var j in this.option.showKey){
					$(trHtmlElement).append("<td>"+ this.getValueByKey(data[i], this.option.showKey[j]) + "</td>");
				}
			}else{
				for(var j in data[i]){
					$(trHtmlElement).append("<td>"+data[i][j]+"</td>");
				}
			}
			
			
			if(this.option.buttons && this.option.buttons.length > 0){
				//生成操作按钮
				var tdHtmlBtnElement = document.createElement("td");
				for(var j = 0; j < this.option.buttons.length; j ++){
					var inputElement = document.createElement("input");
					$(inputElement).attr("type", "button");
					$(inputElement).attr("class", this.option.buttons[j].class);
					$(inputElement).attr("style", this.option.buttons[j].style);
					$(inputElement).attr("value", this.option.buttons[j].name);
					var self = this.option.buttons;
					$(inputElement).attr("selfData", JSON.stringify(data[i]));
					$(inputElement).attr("selfbtnI", j);
					$(inputElement).on("click", function(e){
						self[$(this).attr("selfbtnI")].click(JSON.parse($(this).attr("selfData")));
					});
					$(tdHtmlBtnElement).append(inputElement);
				}
				$(trHtmlElement).append(tdHtmlBtnElement);
			}
			
			$("#" + id).append(trHtmlElement);
		}
	}
	/**
	 * 根据key获取对象的值 obj={person:{name:"张三", age:23}} key="person.name" => 张三 key="person.age" => 23
	 * @method getValueByKey
	 * @param {obj} 对象. {key} 值
	 * @return 获取的值
	 **/
	_prote.getValueByKey = function(obj, key){
		if(key == undefined || key == "" || key == null){
			return obj;
		}
		var keys = key.split(".");
		var value = obj;

		for(var i = 0; value && i < keys.length; i ++){
			value = value[keys[i]];
		}
		return value;
	}
	
	_prote.tempLeftCascadeHtml = ""; //保存生成的html
	/**
	 * 渲染级联html
	 * @method renderLeftCascade
	 * @param {id} 参数.
	 * @return
	 **/
	_prote.renderLeftCascade = function(id){
		tempLeftCascadeHtml = "<tr>";
		$("#" + id).html("");
		this.genLeftCascadeHtml(this.option, id);
	}


	/**
	 * 根据参数option递归生成级联Html,将生成的Html保存到tempLeftCascadeHtml变量
	 * @method genLeftCascadeHtml
	 * @param {option} 参数.
	 * @return
	 **/
	_prote.genLeftCascadeHtml = function(option, id){
		if(option.data && option.data.length > 0 && !option.stopCascade){ //级联显示
			for(var i = 0; i < option.data.length; i ++){
				var data = option.data[i];
				var style = "";
				var stclass = "";
				if(data.style){
					style = data.style;
				}
				if(data.class){
					stclass = data.class;
				}
				if(i == 0){
					tempLeftCascadeHtml += "<td class='"+stclass+"' style='"+style+"' rowspan="+data.size+">"+data.name+"</td>"
				}else{
					tempLeftCascadeHtml += "<tr><td class='"+stclass+"' style='"+style+"' rowspan="+data.size+">"+data.name+"</td>"
				}
				this.genLeftCascadeHtml(data, id);
			}
		}else if(option.data && option.data.length > 0 && option.stopCascade){ //级联结束 渲染后面的数据
			for(var i = 0; i < option.data.length; i ++){
				var data = option.data[i];
				var style = "";
				var stclass = "";
				if(data.style){
					style = data.style;
				}
				if(data.class){
					stclass = data.class;
				}
				tempLeftCascadeHtml += "<td class='"+stclass+"' style='"+style+"'>"+ this.getValueByKey(data, this.option.stopShowKey)+"</td>"
			}
			
			tempLeftCascadeHtml += "</tr>";
			var tempLeftCascadeHtmlDom = $(tempLeftCascadeHtml);
			if(this.option.buttons && this.option.buttons.length > 0){
				//生成操作按钮
				var tdHtmlBtnElement = document.createElement("td");
				for(var j = 0; j < this.option.buttons.length; j ++){
					var inputElement = document.createElement("input");
					$(inputElement).attr("type", "button");
					$(inputElement).attr("class", this.option.buttons[j].class);
					$(inputElement).attr("style", this.option.buttons[j].style);
					$(inputElement).attr("value", this.option.buttons[j].name);
					var self = this.option.buttons;
					$(inputElement).attr("selfData", JSON.stringify(option));
					$(inputElement).attr("selfbtnI", j);
					$(inputElement).on("click", function(e){
						self[$(this).attr("selfbtnI")].click(JSON.parse($(this).attr("selfData")));
					});
					$(tdHtmlBtnElement).append(inputElement);
				}
				$(tempLeftCascadeHtmlDom).append(tdHtmlBtnElement);
			}
			
			$("#" + id).append(tempLeftCascadeHtmlDom);
			tempLeftCascadeHtml = "";
			
		}else{ //结尾的
			tempLeftCascadeHtml += "</tr>";
			var tempLeftCascadeHtmlDom = $(tempLeftCascadeHtml);
			if(this.option.buttons && this.option.buttons.length > 0){
				//生成操作按钮
				var tdHtmlBtnElement = document.createElement("td");
				for(var j = 0; j < this.option.buttons.length; j ++){
					var inputElement = document.createElement("input");
					$(inputElement).attr("type", "button");
					$(inputElement).attr("class", this.option.buttons[j].class);
					$(inputElement).attr("style", this.option.buttons[j].style);
					$(inputElement).attr("value", this.option.buttons[j].name);
					var self = this.option.buttons;
					$(inputElement).attr("selfData", JSON.stringify(option));
					$(inputElement).attr("selfbtnI", j);
					$(inputElement).on("click", function(e){
						self[$(this).attr("selfbtnI")].click(JSON.parse($(this).attr("selfData")));
					});
					$(tdHtmlBtnElement).append(inputElement);
				}
				$(tempLeftCascadeHtmlDom).append(tdHtmlBtnElement);
			}
			
			$("#" + id).append(tempLeftCascadeHtmlDom);
			tempLeftCascadeHtml = "";
		}
	}

	/**
	 * 为级联option每个元素设置size, size为其data里最底部元素的个数
	 * @method setLeftCascadeSize
	 * @param {option} 参数.
	 * @return {option} 参数.
	 **/
	_prote.setLeftCascadeSize = function(option){
		option.size = this.calculationLeftCascadeSize(option);
		for(var i = 0; i < option.data.length; i ++){
			option.data[i].size = this.calculationLeftCascadeSize(option.data[i]);
			if(option.data[i].data){
				option.data[i] = this.setLeftCascadeSize(option.data[i]);
			}
		}
		return option;
	}
	/**
	 * 计算当前的级联option最底部元素的个数
	 * @method calculationLeftCascadeSize
	 * @param {option} 参数.
	 * @return {sum} 个数.
	 **/
	_prote.calculationLeftCascadeSize = function(option){
		if(option.data && option.data.length > 0 && !option.stopCascade){
			var sum = 0;
			for(var i = 0; i < option.data.length; i ++){
				sum += this.calculationLeftCascadeSize(option.data[i]);
			}
			return sum;
		}else{
			return 1;
		}
	}

	return XTable;
})()